package com.lly.songhigh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lly.songhigh.R;
import com.lly.songhigh.activity.SearchMVDetialsActivity;
import com.lly.songhigh.adapter.SearchMVListViewAdapter;
import com.lly.songhigh.constant.URL;
import com.lly.songhigh.entity.SearchMV;
import com.lly.songhigh.utils.SharedPreferencesUtils;
import com.lly.songhigh.view.SwipeRefreshLoadingLayout;
import com.lly.songhigh.volley.VolleySingleton;

import java.util.List;


/**
 * Created by dllo on 15/10/24.
 */

public class SearchMVFragment extends BaseFragment implements AdapterView.OnItemClickListener, SwipeRefreshLoadingLayout.OnLoadListener, SwipeRefreshLoadingLayout.OnRefreshListener {
//    @Bind(R.id.mv_listView)
//    ListView mvListView;

    private ListView mvListView;
    private String startUrl = URL.SEARCHMV;
    private String endUrl ;
    private String url;
    private int pageCount, page = 1;
    private SwipeRefreshLoadingLayout refreshLoadingLayout;



    private List<SearchMV.DataEntity> datas;
    private SearchMVListViewAdapter searchMVListViewAdapter;
    private VolleySingleton volleySingleton;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_mv, null);


        refreshLoadingLayout = (SwipeRefreshLoadingLayout) view.findViewById(R.id.mv_refreshLoadingLayout);
        refreshLoadingLayout.setOnLoadListener(this);
        refreshLoadingLayout.setOnRefreshListener(this);

        mvListView = (ListView) view.findViewById(R.id.mv_listView);
        mvListView.setOnItemClickListener(this);
        volleySingleton = VolleySingleton.getInstance();
        endUrl = SharedPreferencesUtils.getSharePreStr(getActivity(), "SearchActivityEditText", "endUrl");
        url = startUrl + endUrl;
        Log.i("lhl1", "====" + url);
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SearchMV searchMV = gson.fromJson(response, SearchMV.class);
                datas = searchMV.getData();
                searchMVListViewAdapter = new SearchMVListViewAdapter(datas, getActivity());
                mvListView.setAdapter(searchMVListViewAdapter);
                pageCount = searchMV.getPages();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SearchMV.DataEntity dataEntity = (SearchMV.DataEntity) parent.getItemAtPosition(position);
        int urlId = dataEntity.getId();
        Intent intent = new Intent(getActivity(), SearchMVDetialsActivity.class);
        intent.putExtra("_id", urlId);
        getActivity().startActivity(intent);
    }

    @Override
    public void onLoad() {
        page++;
        if (page > pageCount) {
            refreshLoadingLayout.setLoading(false);
        } else{
            url = URL.SEARCHMV_LOADURL + page + "&q=" + endUrl;
            StringRequest request = new StringRequest(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    SearchMV searchMV = gson.fromJson(response, SearchMV.class);
                    datas = searchMV.getData();
                    searchMVListViewAdapter.loadData(datas);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

            volleySingleton.addQueue(request);
            refreshLoadingLayout.setLoading(false);
        }

    }

    @Override
    public void onRefresh() {
        refreshLoadingLayout.setRefreshing(false);
    }
}
